package cn.wolfcode.trip.admin.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 文件上传工具
 */
public class QiNiuUploadUtil {
    //七牛上传参数
    private static String accessKey = "HIzcDiXjuDUDsT8ooC4YNDKyuYEsKocQjtM-Wx4G";
    private static String secretKey = "yI2sVErwfSqpZzK8kTM_mWX-YyGDlqvfSUcxQC-E";
    private static String bucket = "photos";
    private static String domainName = "http://plav6c5na.bkt.clouddn.com";

    /**
     * 处理文件上传
     *
     * @param file //     * @param basePath 存放文件的目录的绝对路径 servletContext.getRealPath("/upload")
     * @return 图片地址
     */
    public static String upload(MultipartFile file) {
        //处理上传文件名
        String uuid = UUID.randomUUID().toString();
        String orgFileName = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(orgFileName);
        String fileName = uuid + ext;
        //上传
        try {
            DefaultPutRet defaultPutRet = fileUpload(
                    Zone.zone0(), getUploadCredential(), file.getBytes(), fileName);
            return domainName + "/" + defaultPutRet.key;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String upload(MultipartFile file, String basePath) {
        return upload(file);
    }

    /*TODO 文件删除方法*/

    /**
     * 文件上传
     *
     * @param zone    华东    Zone.zone0()
     *                华北    Zone.zone1()
     *                华南    Zone.zone2()
     *                北美    Zone.zoneNa0()
     * @param upToken 上传凭证
     *                //     * @param file    需要上传的文件本地路径
     * @return 文件地址
     */
    public static DefaultPutRet fileUpload(Zone zone, String upToken, byte[] fileBytes, String fileName) {
        DefaultPutRet putRet = null;

        // 构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(zone);
        // ...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        // 默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = fileName;
        try {
            Response response = uploadManager.put(fileBytes, key, upToken);
            // 解析上传成功的结果
            putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println("putRet.key:" + putRet.key);
            System.out.println("putRet.hash:" + putRet.hash);
            return putRet;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                // ignore
            }
        }
        return putRet;
    }

    /**
     * 获取上传凭证
     */
    public static String getUploadCredential() {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        System.out.println("upToken:" + upToken);
        return upToken;
    }
}























